﻿using System;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;

namespace Commlibrary
{
    public class Utility
    {
        private static string contentType = "application/x-www-form-urlencoded";
        private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
        private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
        private static Encoding encoding = Encoding.GetEncoding("GB18030");
        
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="sSrcValue">字符串</param>
        /// <param name="sStartValue">开始字符串</param>
        /// <param name="sEndValue">结束字符串</param>
        /// <returns>介于开始和结束字符串之间的字符串</returns>
        public static string GetMidString(String sSrcValue, String sStartValue, String sEndValue)
        {
            if (string.IsNullOrEmpty(sSrcValue) || 
                string.IsNullOrEmpty(sStartValue) || 
                string.IsNullOrEmpty(sEndValue))
            {
                return string.Empty;
            }

            int iFirst = sSrcValue.IndexOf(
                sStartValue, StringComparison.CurrentCultureIgnoreCase);

            if (iFirst == -1)
            {
                return string.Empty;
            }

            iFirst += sStartValue.Length;
            int iEnd = sSrcValue.IndexOf(sEndValue, iFirst, StringComparison.CurrentCultureIgnoreCase);

            if (iEnd == -1)
            {
                return string.Empty;
            }

            return sSrcValue.Substring(iFirst, iEnd - iFirst);
        }

        public static ArrayList GetMidStringList(String sSrcValue, String sStartValue, String sEndValue)
        {
            if (String.IsNullOrEmpty(sSrcValue) ||
                String.IsNullOrEmpty(sStartValue) ||
                String.IsNullOrEmpty(sEndValue))
            {
                return null;
            }

            ArrayList arrMidString = new ArrayList();

            int iFirst = sSrcValue.IndexOf(
                sStartValue, StringComparison.CurrentCultureIgnoreCase);

            while (true)
            {
                String sFindString = 
                    Utility.GetMidString(sSrcValue, sStartValue, sEndValue);

                if (sFindString.Length > 0)
                {
                    arrMidString.Add(sFindString);
                }
                else
                {
                    break;
                }

                sSrcValue = sSrcValue.Replace(sStartValue + sFindString + sEndValue, string.Empty);
            }

            return arrMidString;
        }


        /// <summary>
        /// 获取指定页面的HTML代码
        /// </summary>
        /// <param name="url">指定页面的路径</param>
        /// <returns></returns>
        public static string GetHtml(string url)
        {
            try
            {
                CookieContainer cookieContainer = new CookieContainer();
                HttpWebRequest httpWebRequest;
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = "GET";

                HttpWebResponse httpWebResponse;
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();

                return html;
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
                Console.ForegroundColor = ConsoleColor.White;
                return string.Empty;
            }
        }
    }
}
